﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JoyTalq.Core.Persistence.Ado;
using JoyTalq.Core.Persistence;
using JoyTalq.Core.Business.Domain;
using NHibernate.Criterion;

namespace JoyTalq.Core.Business.Persistence
{
    public class BusinessDao : AdoDao, IBusinessDao
    {
        public INHibernateDao NHibernateDao { get; set; }

        /// <summary>
        /// 新增企業資料主檔
        /// </summary>
        /// <param name="customerVO">被新增的企業資料主檔</param>
        /// <returns>新增後的企業資料主檔</returns>
        public CustomerVO CreateCustomer(CustomerVO customerVO)
        {
            //            string cmdText = @"INSERT INTO CORE_ITEM_PARAM(
            //    Classify, Name, Value, Deleted)
            //VALUES(
            //    @Classify, @Name, @Value, @Deleted) ";

            //            //抓取新增後的id
            //            cmdText += " SELECT SCOPE_IDENTITY() "; 
            //            IDbParameters dbParameters = CreateDbParameters();
            //            dbParameters.Add("Classify", DbType.String).Value = customerVO.Classify;
            //            dbParameters.Add("Name", DbType.String).Value = customerVO.Name;
            //            dbParameters.Add("Value", DbType.String).Value = customerVO.Value;
            //            dbParameters.Add("Deleted", DbType.Boolean).Value = customerVO.Deleted;

            //            int id = Convert.ToInt32(AdoTemplate.ExecuteScalar(CommandType.Text, cmdText, dbParameters));
            //            customerVO.CustomerId = id;

            //            return customerVO;

            NHibernateDao.Insert(customerVO);

            return customerVO;
        }

        /// <summary>
        /// 更新企業資料主檔
        /// </summary>
        /// <param name="customerVO">被更新的企業資料主檔</param>
        /// <returns>更新後的企業資料主檔</returns>
        public CustomerVO UpdateCustomer(CustomerVO customerVO)
        {
            NHibernateDao.Update(customerVO);

            return customerVO;
        }

        /// <summary>
        /// 刪除企業資料主檔
        /// </summary>
        /// <param name="customerVO">被刪除的企業資料主檔</param>
        public void DeleteCustomer(CustomerVO customerVO)
        {
            NHibernateDao.Delete(customerVO);
        }

        /// <summary>
        /// 取得企業資料主檔 By 識別碼
        /// </summary>
        /// <param name="customerId">識別碼</param>
        /// <returns>企業資料主檔</returns>
        public CustomerVO GetCustomerById(Guid customerId)
        {
            return NHibernateDao.GetVOById<CustomerVO>(customerId);
        }

        /// <summary>
        /// 取得企業資料主檔清單
        /// </summary>
        /// <param name="queryString">搜尋語法，用Where...order by ...</param>
        /// <returns>企業資料主檔清單</returns>
        public IList<CustomerVO> GetCustomerList(string queryString)
        {
            return NHibernateDao.SearchByWhere<CustomerVO>(queryString);
        }

        /// <summary>
        /// 取得企業資料主檔清單
        /// </summary>
        /// <param name="queryString">搜尋語法，用Where...order by ...</param>
        /// <param name="pageIndex">分頁索引</param>
        /// <param name="pageSize">分頁大小</param>
        /// <returns>企業資料主檔清單</returns>
        public IList<CustomerVO> GetCustomerList(string queryString, int pageIndex, int pageSize)
        {
            return NHibernateDao.SearchByWhere<CustomerVO>(queryString, pageIndex, pageSize);
        }

        /// <summary>
        /// 取得全部的企業資料主檔清單
        /// </summary>
        /// <returns>全部的企業資料主檔清單</returns>
        public IList<CustomerVO> GetAllCustomerList()
        {
            return NHibernateDao.GetAllVO<CustomerVO>();
        }

        /// <summary>
        /// 取得全部的企業資料主檔清單
        /// </summary>
        /// <param name="pageIndex">分頁索引</param>
        /// <param name="pageSize">分頁大小</param>
        /// <returns>全部的企業資料主檔清單</returns>
        public IList<CustomerVO> GetAllCustomerList(int pageIndex, int pageSize)
        {
            return NHibernateDao.GetAllVO<CustomerVO>(pageIndex, pageSize);
        }

        /// <summary>
        /// 取得全部的企業資料主檔清單
        /// </summary>
        /// <param name="pageIndex">分頁索引</param>
        /// <param name="pageSize">分頁大小</param>
        /// <param name="sortField">排序欄位</param>
        /// <param name="sortDesc">升降冪排序</param>
        /// <returns>全部的企業資料主檔清單</returns>
        public IList<CustomerVO> GetAllCustomerList(int pageIndex, int pageSize, string sortField, bool sortDesc)
        {
            if (sortDesc)
            {
                return NHibernateDao.GetAllVO<CustomerVO>(pageIndex, pageSize, Order.Desc(sortField));
            }
            else
            {
                return NHibernateDao.GetAllVO<CustomerVO>(pageIndex, pageSize, Order.Asc(sortField));
            }
        }

        /// <summary>
        /// 新增品牌資料主檔
        /// </summary>
        /// <param name="brandVO">被新增的品牌資料主檔</param>
        /// <returns>新增後的品牌資料主檔</returns>
        public BrandVO CreateBrand(BrandVO brandVO)
        {
            NHibernateDao.Insert(brandVO);

            return brandVO;
        }

        /// <summary>
        /// 更新品牌資料主檔
        /// </summary>
        /// <param name="brandVO">被更新的品牌資料主檔</param>
        /// <returns>更新後的品牌資料主檔</returns>
        public BrandVO UpdateBrand(BrandVO brandVO)
        {
            NHibernateDao.Update(brandVO);

            return brandVO;
        }

        /// <summary>
        /// 刪除品牌資料主檔
        /// </summary>
        /// <param name="brandVO">被刪除的品牌資料主檔</param>
        public void DeleteBrand(BrandVO brandVO)
        {
            NHibernateDao.Delete(brandVO);
        }

        /// <summary>
        /// 取得品牌資料主檔 By 識別碼
        /// </summary>
        /// <param name="brandId">識別碼</param>
        /// <returns>品牌資料主檔</returns>
        public BrandVO GetBrandById(Guid brandId)
        {
            return NHibernateDao.GetVOById<BrandVO>(brandId);
        }
    }
}
